import React, { useState } from 'react';
import { Button, message, Modal, Spin } from 'antd';
import axios from 'axios';

function AttendanceInit() {
  const [loading, setLoading] = useState(false);

  const handleCreateHistorical = async () => {
    try {
      const result = await Modal.confirm({
        title: '初始化考勤记录',
        content: '这将为所有用户创建历史考勤记录，是否继续？',
        okText: '确定',
        cancelText: '取消'
      });

      if (result) {
        setLoading(true);
        const response = await axios.post('http://localhost:3111/attendance/create-historical');
        
        if (response.data.code === 200) {
          message.success(response.data.message);
        } else {
          message.error(response.data.message);
        }
      }
    } catch (error) {
      message.error(error.response?.data?.message || '操作失败，请重试');
    } finally {
      setLoading(false);
    }
  };

  return (
    <div style={{ 
      padding: '20px',
      display: 'flex',
      flexDirection: 'column',
      alignItems: 'center',
      gap: '20px'
    }}>
      <h2>考勤记录初始化</h2>
      <Spin spinning={loading}>
        <Button 
          type="primary" 
          onClick={handleCreateHistorical}
          size="large"
          loading={loading}
        >
          {loading ? '初始化中...' : '开始初始化考勤记录'}
        </Button>
      </Spin>
    </div>
  );
}

export default AttendanceInit;